//
//  MainGameViewController.m
//  myGames
//
//  Created by binbinming on 09-12-26.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MainGameViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation MainGameViewController

@synthesize shootBtn;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)viewDidLoad {
    [super viewDidLoad];
	UIImage *mainImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"gamebackground" ofType:@"jpg"]];//也可以直接用imagewithName,从mainbundle中加载
	UIImageView *mainview = [[UIImageView alloc] initWithImage:mainImage];
	[self.view insertSubview:mainview atIndex:0];
	
	[self addSheeps:mainview];
	//add uibutton background image
	[self.shootBtn setImage:[UIImage imageNamed:@"shootBtn.png"] forState:UIControlStateNormal];
	
	//add NSTimer schedule to make a animation from here to there.
	//NSTimer *runTimer = [[NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
	//													selector:@selector(onSheepRunTimer:) 
	//
	//													userInfo:mainview repeats:YES] retain];
	
	
	
	[mainImage release];
	[mainview release];
	[shootBtn release];
}

-(void)onSheepRunTimer:(NSTimer *)timer{
	//[[[(UIImageView *)[timer userInfo] viewWithTag:0] frame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>) ]]
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

-(void)addSheeps:(UIImageView *)mainview{
	UIImage *sheep1 = [UIImage imageNamed:@"sheep_eat.gif"];
	UIImageView *sheepView = [[UIImageView alloc] initWithImage:sheep1];
	[mainview addSubview:sheepView];
	
	//add animation
	CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
	theAnimation.duration = 1;
	theAnimation.fromValue = [NSNumber numberWithFloat:0];
	theAnimation.toValue = [NSNumber numberWithFloat:200];
	[sheepView.layer addAnimation:theAnimation forKey:@"sheepRunanimation"];
	
	[sheepView release];
}

- (void)dealloc {
    [super dealloc];
}

-(IBAction)shoot:(id)sender{
	
		
}

/*
 * 递归查询view
 */
- (UIView*) findView:(UIView*)view  
{  
    Class clazz = [view class];  
    NSString *desc = [clazz description];  
    if ([desc compare:@"UIButton"] == NSOrderedSame)  
    {  
        return view;  
    }  
	
    for (UIView *sub in [view subviews])  
    {  
        if ([self findView:sub])  
        {  
            return sub;  
        }  
    }  
	
    return nil;  
}  


@end
